package dominion.model.actions.base;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

import dominion.model.Card;
import dominion.model.GameBoard;
import dominion.model.Player;
import dominion.model.actions.Action;
import dominion.model.monies.Coin;
import dominion.utils.Constants;

public class Mine extends Action {

	public Mine() {
		this.cost = 5;
	}

	@Override
	public Card clone() {
		return new Mine();
	}

	@Override
	public String toString() {
		return Constants.MINE;
	}

	@Override
	public String getDescription() {
		StringBuffer sb = new StringBuffer();
		sb.append(super.getDescription());
		sb.append("Trash a treasure card from your hand.\n");
		sb.append("Gain a treasure card costing up to $3 more and put it into your hand.\n");
		return sb.toString();
	}

	@Override
	public void doAction(Player player, GameBoard board, List<Player> players) throws IOException {
		if (player.getHand().size() == 1)
		{
			Constants.printer.print("Not enough cards in hand.");
			return;
		}
		playedCard(player);
		player.changeAction(-1);
		Constants.printer.print("Type a card to trash or none to skip.");
		Constants.printer.print(player.printHand());

		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		String input = null;
		boolean done = false;
		String choice = null;
		while (!done && !player.getHand().isEmpty())
		{
			input = in.readLine().trim();
			if (input.equalsIgnoreCase("none"))
			{
				done = true;
			}
			else if (player.getHand().contains(input))
			{
				Card card = player.getHand().getCard(input);
				if (card instanceof Coin)
				{
					card = player.drawFromDeck(card.toString(), Constants.HAND);
					choice = card.toString();
					board.addToTrash(card);
					done = true;
				}
				else
				{
					Constants.printer.print("Choice must be a treasure card.");
				}
			}
			else
			{
				Constants.printer.print("Card does not exist in hand.");
			}
		}

		Constants.printer.print("What treasure would you like to gain?");
		done = false;
		while (!done)
		{
			input = in.readLine().trim();
			if (input.equalsIgnoreCase("Copper"))
			{
				Card card = board.drawFromCopper();
				if (card == null)
				{
					Constants.printer.print("Copper is empty, returning Mine!");
					player.changeAction(1);
					done = true;
				}
				else
				{
					player.addToDeck(card, Constants.HAND);
					done = true;
				}
			}
			else if (input.equalsIgnoreCase("Silver"))
			{
				Card card = board.drawFromSilver();
				if (card == null)
				{
					Constants.printer.print("Silver is empty, returning Mine!");
					player.changeAction(1);
					done = true;
				}
				else
				{
					player.addToDeck(card, Constants.HAND);
					done = true;
				}
			}
			else if (input.equalsIgnoreCase("Gold"))
			{
				Card card = board.drawFromGold();
				if (choice.equalsIgnoreCase("Copper"))
				{
					Constants.printer.print("You trashedd a Copper, you may not pick a Gold!");
				}
				else if (card == null)
				{
					Constants.printer.print("Gold is empty, returning Mine!");
					player.changeAction(1);
					done = true;
				}
				else
				{
					player.addToDeck(card, Constants.HAND);
					done = true;
				}
			}
			else
			{
				Constants.printer.print("Invalid choice.");
			}
		}
		Constants.printer.print("New hand: ");
		Constants.printer.print(player.printHand());
	}
}
